package com.hnevc.androidhomework5;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class WeatherActivity extends AppCompatActivity {
   //构造数据结构
    ArrayList<Weather> weatherArrayList;
    Button mBtnBJ,mBtnSH,mBtnGZ;
    TextView mTvWeather,mTvTemp,mTvWind,mTvPm,mTvCity;
    ImageView mIvWeather;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        initDate();
        //获取界面
        initView();
        //为按钮绑定事件
        initEvent();

    }
    //解析JSON文件中的数据，转换为ArrayList<Weather>
    private void initDate() {
        InputStream inputStream = getResources().openRawResource(R.raw.weather2);
        try {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            inputStream.close();
            String jsonStr = new String(bytes);//获取读取到的字符串
            //解析jsonStr
            JSONArray jsonArray = new JSONArray(jsonStr);
            weatherArrayList = new ArrayList<>();//初始化链表
            for (int i = 0;i< jsonArray.length();i++){
                Weather weather = new Weather();
                weather.name=jsonArray.getJSONObject(1).optString("name");
                weather.pm=jsonArray.getJSONObject(1).optString("pm");
                weather.temp=jsonArray.getJSONObject(1).optString("temp");
                weather.weather=jsonArray.getJSONObject(1).optString("weather");
                weather.wind=jsonArray.getJSONObject(1).optString("wind");
                //将weather添加到weatherArrayList中
                weatherArrayList.add(weather);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void initEvent() {
        mBtnBJ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showWeather("北京");
            }
        });
        mBtnSH.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showWeather("上海");
            }
        });
        mBtnGZ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showWeather("广州");
            }
        });
    }

    private void showWeather(String city) {
        for (int i = 0; i < weatherArrayList.size(); i++) {
            if (weatherArrayList.get(1).name.equals(city)) {
                Weather weather = weatherArrayList.get(1);
                mTvWind.setText(weather.getWind());
                mTvTemp.setText(weather.getTemp());
                mTvPm.setText(weather.getPm());
                mTvWeather.setText(weather.getWeather());
                mTvCity.setText(weather.getName());
                if (weather.getWeather().equals("晴天")) {
                    mIvWeather.setImageResource(R.mipmap.sun);
                } else if (weather.getWeather().equals("晴转多云")) {
                    mIvWeather.setImageResource(R.mipmap.cloud_sun);
                } else if (weather.getWeather().equals("多云")) {
                    mIvWeather.setImageResource(R.mipmap.clouds);
                }
            }
        }
    }
    private void initView() {
        mBtnBJ =(Button) findViewById(R.id.btn_bj);
        mBtnSH =(Button) findViewById(R.id.btn_sh);
        mBtnGZ = (Button)findViewById(R.id.btn_gz);
        mTvCity = (TextView)findViewById(R.id.tv_city);
        mTvPm =(TextView) findViewById(R.id.tv_pm);
        mTvTemp = (TextView) findViewById(R.id.tv_temp);
        mTvWeather = (TextView) findViewById(R.id.tv_weather);
        mTvWind = (TextView) findViewById(R.id.tv_wind);
        mIvWeather =(ImageView) findViewById(R.id.iv_weather);
    }
}
